package cn.elead.chaos.core.map;

import java.util.Map;

/**
 * 
 * @Auth 赵光
 * @Describle 复合Map，等价于Map<String, Map<K, V>>, 功能增强(数据结构特别复杂建议采用DataMap)
 */
public class MultiMap<K, V> extends DefaultMap<Map<K, V>> {

	private static final long serialVersionUID = 1L;

	public MultiMap() {

	}

	public MultiMap(String key, Map<K, V> value) {
		put(key, value);
	}

	public MultiMap(Map<String, Map<K, V>> map) {
		putAll(map);
	}

	public MultiMap<K, V> addValue(String cacheKey, K key, V value) {
		Map<K, V> map = get(key);
		if (map == null) {
			map = Maps.newMap();
		}
		map.put(key, value);
		put(cacheKey, map);
		return this;
	}

	@SuppressWarnings("unchecked")
	public <R> R get(String cacheKey, K key) {
		if (containsKey(cacheKey)) {
			return (R) get(cacheKey).get(key);
		}
		return null;
	}

	public static <K, V> MultiMap<K, V> newMap() {
		return new MultiMap<>();
	}

	public static <K, V> MultiMap<K, V> newMap(String cacheKey, K key, V value) {
		return new MultiMap<K, V>().addValue(cacheKey, key, value);
	}

	public static <K, V> MultiMap<K, V> newMap(Map<String, Map<K, V>> map) {
		return new MultiMap<>(map);
	}
}
